var GAME_STATE = {
	WAITTING: "-1", //等待
	START: "0", //开始
	END: "1", //结束
	PAUSE: "2", //暂停
	RESUME: "3", //恢复
};

cc.Class({
	extends: cc.Component,

	properties: {
		//手控制的球
		ball_self: {
			type: cc.Node,
			default: null,
		},
		//受保护的球
		ball_protect: {
			type: cc.Node,
			default: null,
		},

		//障碍物父节点
		layout: {
			type: cc.Node,
			default: null,
		},

		score_now: {
			type: cc.Label,
			default: null,
		},
		score_hight: {
			type: cc.Label,
			default: null,
		},
		end_game: {
			type: cc.Node,
			default: null,
		},
		start_game: {
			type: cc.Node,
			default: null,
		},
		//敌人的组
		enemy_zu: {
			type: cc.Prefab,
			default: [],
		},
	},



	onLoad() {
		this.ball_self.getComponent("ball_self").init(this); //初始化控制的球
		this.ball_protect.getComponent("ball_protect").init(this); //初始化控制的球
		this.geme_state = GAME_STATE.WAITTING; //等待状态
		this.end_game.active = false;
		this.start_game.active = true;
		cc.jsInstance = {};
		//音频管理类
		var AudioManager = require("audioMgr");
		cc.jsInstance.audioManager = new AudioManager();
		cc.jsInstance.audioManager.init();
		cc.jsInstance.audioManager.playBGM("bg");
		this.ball_self_Pos = this.ball_self.getPosition();
	},

	//初始化游戏数据
	init() {
		this.ball_self.setPosition(this.ball_self_Pos.x, this.ball_self_Pos.y);
		//敌人父节点清空
		this.layout.removeAllChildren();
		//隐藏结束和开始节点
		this.end_game.active = false;
		this.start_game.active = false;
		//初始化分数显示
		this.score = 0;
		if (cc.sys.localStorage.getItem("hignt_score")) {
			this.score_hight.string = cc.sys.localStorage.getItem("hignt_score");
		} else {
			this.score_hight.string = "0";
		}
		this.score_now.string = this.score;

	},

	random_enemy() {
		this.callback = function() {
			// if (this.geme_state === GAME_STATE.END) {
			// 	this.unschedule(this.callback);
			// }
			//生产随机敌人组 并添加到敌人父节点下
			var random = Math.floor(Math.random() * 6); // 0 ,5
			var enemy = cc.instantiate(this.enemy_zu[random]);
			this.layout.addChild(enemy);
		}.bind(this);
		this.schedule(this.callback, 4, cc.macro.REPEAT_FOREVER, 0); // 默认值为永远执行，马上开始  schedule(函数, 多长时间掉一次, 次数(永远), 隔多少秒以后开始执行shedule)
	},

	//自己控制的球碰到障碍物 加分
	addScore() {
		// console.log("加分-----");
		this.score++;
		this.score_now.string = this.score + "";
		//随机播放一个音效
		this.playRandomSfx();
	},


	gameOver() {
		console.log("游戏结束-----");
		this.layout.removeAllChildren();
		this.geme_state = GAME_STATE.END; //结束状态
		this.unschedule(this.callback);
		var hight_score = cc.sys.localStorage.getItem("hignt_score")
		if (hight_score) {
			if (parseInt(hight_score) < this.score) {
				cc.sys.localStorage.setItem("hignt_score", this.score + ""); //加入缓存
			}
		} else {
			cc.sys.localStorage.setItem("hignt_score", this.score + ""); //加入缓存
		}
		this.score_hight.string = cc.sys.localStorage.getItem("hignt_score");
		this.score = 0;
		this.playMissSfx(); //播放结束音效
		//显示结束页面
		this.end_game.active = true;
	},

	//点击游戏开始
	gameStart() {
		console.log("游戏开始-----");
		this.init();
		this.geme_state = GAME_STATE.START; //开始状态
		//随机出现障碍物组
		this.random_enemy();
	},
	gameReStart() {
		console.log("重新开始-----");
		this.init();
		this.geme_state = GAME_STATE.START; //开始状态
		//随机出现障碍物组
		this.random_enemy();
	},
	return_home() {
		this.init();
		this.end_game.active = false;
		this.start_game.active = true;

	},

	//播放正确碰撞的音效
	playRandomSfx() {
		var random = Math.floor(Math.random() * 4) + 1; // 01-14
		cc.jsInstance.audioManager.playSFX(random + "");
	},

	//播放结束碰撞错误的声音
	playMissSfx() {
		cc.jsInstance.audioManager.playBtnClick("error");
	},

	update(dt) {},
});